#ifndef __KEYLOGGER_H__
#define __KEYLOGGER_H__

#include <X11/Xlib.h>
#include <X11/extensions/record.h>
#include <X11/extensions/XTest.h>
#include <X11/keysymdef.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <X11/Xlibint.h>

#include <ctime>
#include <iostream>
#include <stdexcept>

using namespace::std;

typedef union
{
	unsigned char type;
	xEvent event;
	xResourceReq req;
	xGenericReply reply;
	xError error;
	xConnSetupPrefix setup;
} XRecordDatum;

struct CallbackClosure
{
	Display *ctrlDisplay;
	Display *dataDisplay;
	int *startWorkTimestamp;
	int *stopWorkTimestamp;
};


class KKeylogger
{
public:
	KKeylogger(int *startWorkTimestamp, int *stopWorkTimestamp);
	~KKeylogger();
	void start();
	void stop();
	bool xConnect(const string &displayName);
	void processData();
	void setupRecordExtension();
	
private:
	XRecordContext m_recordContext;
	XRecordRange *m_recordRange;
	XRecordClientSpec m_recordClientSpec;
	CallbackClosure m_userData;
	static void eventCallback(XPointer priv, XRecordInterceptData *hook);

};

#endif
